home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / mpeg_stat-2.2 / decoders.h < prev    next >
C/C++ Source or Header  |  1995-05-10  |  23KB  |  498 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  *
  3.  *  Copyright (c) 1995 The Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Technical University of Berlin, Germany, Dept. of Computer Science
  7.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  8.  *
  9.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  10.  *
  11.  * This program uses parts of the source code of the Berkeley MPEG player
  12.  *
  13.  * ---------------------------
  14.  *
  15.  * Copyright (c) 1993 Technical University of Berlin, Germany
  16.  *
  17.  * for the parts of the Berkeley player used:
  18.  *
  19.  * Copyright (c) 1992 The Regents of the University of California.
  20.  * All rights reserved.
  21.  *
  22.  * ---------------------------
  23.  *
  24.  * Permission to use, copy, modify, and distribute this software and its
  25.  * documentation for any purpose, without fee, and without written agreement is
  26.  * hereby granted, provided that the above copyright notices and the following
  27.  * two paragraphs appear in all copies of this software.
  28.  * 
  29.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  30.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  31.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  32.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  33.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  34.  * POSSIBILITY OF SUCH DAMAGE.
  35.  * 
  36.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  37.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  38.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  40.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  41.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  42.  * OR MODIFICATIONS.
  43.  */
  44. /*
  45.  * decoders.h
  46.  *
  47.  * This file contains the declarations of structures required for Huffman
  48.  * decoding
  49.  *
  50.  */
  51.  
  52. /* Include util.h for bit i/o parsing macros. */
  53.  
  54. #include "util.h"
  55.  
  56. /* Code for unbound values in decoding tables */
  57. #define ERROR     (-1)
  58. #define DCT_ERROR 63
  59.  
  60. #define MACRO_BLOCK_STUFFING 34
  61. #define MACRO_BLOCK_ESCAPE 35
  62.  
  63. /* Two types of DCT Coefficients */
  64. #define DCT_COEFF_FIRST 0
  65. #define DCT_COEFF_NEXT 1
  66.  
  67. /* Special values for DCT Coefficients */
  68. #define END_OF_BLOCK 62
  69. #define ESCAPE 61
  70.  
  71. /* Structure for an entry in the decoding table of 
  72.  * macroblock_address_increment */
  73. typedef struct {
  74.   unsigned int value;       /* value for macroblock_address_increment */
  75.   int num_bits;             /* length of the Huffman code */
  76. } mb_addr_inc_entry;
  77.  
  78. /* Decoding table for macroblock_address_increment */
  79. extern mb_addr_inc_entry mb_addr_inc[2048];
  80.  
  81.  
  82. /* Structure for an entry in the decoding table of macroblock_type */
  83. typedef struct {
  84.   unsigned int mb_quant;              /* macroblock_quant */
  85.   unsigned int mb_motion_forward;     /* macroblock_motion_forward */
  86.   unsigned int mb_motion_backward;    /* macroblock_motion_backward */
  87.   unsigned int mb_pattern;            /* macroblock_pattern */
  88.   unsigned int mb_intra;              /* macroblock_intra */
  89.   int num_bits;                       /* length of the Huffman code */
  90. } mb_type_entry;
  91.  
  92. /* Decoding table for macroblock_type in predictive-coded pictures */
  93. extern mb_type_entry mb_type_P[64];
  94.  
  95. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  96. extern mb_type_entry mb_type_B[64];
  97.  
  98.  
  99. /* Structures for an entry in the decoding table of coded_block_pattern */
  100. typedef struct {
  101.   unsigned int cbp;            /* coded_block_pattern */
  102.   int num_bits;                /* length of the Huffman code */
  103. } coded_block_pattern_entry;
  104.  
  105. /* External declaration of coded block pattern table. */
  106.  
  107. extern coded_block_pattern_entry coded_block_pattern[512];
  108.  
  109.  
  110.  
  111. /* Structure for an entry in the decoding table of motion vectors */
  112. typedef struct {
  113.   int code;              /* value for motion_horizontal_forward_code,
  114.                           * motion_vertical_forward_code, 
  115.                           * motion_horizontal_backward_code, or
  116.                           * motion_vertical_backward_code.
  117.                           */
  118.   int num_bits;          /* length of the Huffman code */
  119. } motion_vectors_entry;
  120.  
  121.  
  122. /* Decoding table for motion vectors */
  123. extern motion_vectors_entry motion_vectors[2048];
  124.  
  125.  
  126. /* Structure for an entry in the decoding table of dct_dc_size */
  127. typedef struct {
  128.   unsigned int value;    /* value of dct_dc_size (luminance or chrominance) */
  129.   int num_bits;          /* length of the Huffman code */
  130. } dct_dc_size_entry;
  131.  
  132. /* External declaration of dct dc size lumiance table. */
  133.  
  134. extern dct_dc_size_entry dct_dc_size_luminance[128];
  135.  
  136. /* External declaration of dct dc size chrom table. */
  137.  
  138. extern dct_dc_size_entry dct_dc_size_chrominance[256];
  139.  
  140.  
  141. /* DCT coeff tables. */
  142.  
  143. #define RUN_MASK 0xfc00
  144. #define LEVEL_MASK 0x03f0
  145. #define NUM_MASK 0x000f
  146. #define RUN_SHIFT 10
  147. #define LEVEL_SHIFT 4
  148.  
  149. /* External declaration of dct coeff tables. */
  150.  
  151. extern unsigned short int dct_coeff_tbl_0[256];
  152. extern unsigned short int dct_coeff_tbl_1[16];
  153. extern unsigned short int dct_coeff_tbl_2[4];
  154. extern unsigned short int dct_coeff_tbl_3[4];
  155. extern unsigned short int dct_coeff_next[256];
  156. extern unsigned short int dct_coeff_first[256];
  157.  
  158. #define DecodeDCTDCSizeLum(macro_val)                    \
  159. {                                                    \
  160.   unsigned int index;                                \
  161.                                                      \
  162.   show_bits7(index);                              \
  163.                                                      \
  164.   macro_val = dct_dc_size_luminance[index].value;       \
  165.                                                      \
  166.   flush_bits(dct_dc_size_luminance[index].num_bits); \
  167. }
  168.  
  169. #define DecodeDCTDCSizeChrom(macro_val)                      \
  170. {                                                        \
  171.   unsigned int index;                                    \
  172.                                                          \
  173.   show_bits8(index);                                  \
  174.                                                          \
  175.   macro_val = dct_dc_size_chrominance[index].value;         \
  176.                                                          \
  177.   flush_bits(dct_dc_size_chrominance[index].num_bits);   \
  178. }
  179.  
  180. #define decoders_assert(expression)\
  181.   if (!(expression)) {\
  182.   sprintf (errorSpecifics,"\nBad DCT-decode value in ParseReconBlock\n");\
  183.   return SKIP_TO_START_CODE;}
  184.  
  185.  
  186. #define DecodeDCTCoeff(dct_coeff_tbl, run, level)                       \
  187. {                                                                       \
  188.   unsigned int temp, index;                                             \
  189.   unsigned int value, next32bits, flushed;                              \
  190.                                                                         \
  191.   /*                                                                    \
  192.    * Grab the next 32 bits and use it to improve performance of         \
  193.    * getting the bits to parse. Thus, calls are translated as:          \
  194.    *                                                                    \
  195.    *    show_bitsX  <-->   next32bits >> (32-X)                         \
  196.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);          \
  197.    *                       flushed += X;                                \
  198.    *                       next32bits &= bitMask[flushed];              \
  199.    *    flush_bitsX <-->   flushed += X;                                \
  200.    *                       next32bits &= bitMask[flushed];              \
  201.    *                                                                    \
  202.    * I've streamlined the code a lot, so that we don't have to mask     \
  203.    * out the low order bits and a few of the extra adds are removed.    \
  204.    */                                                                   \
  205.   show_bits32(next32bits);                                              \
  206.                                                                         \
  207.   /* show_bits8(index); */                                              \
  208.   index = next32bits >> 24;                                             \
  209.                                                                         \
  210.   if (index > 3) {                                                      \
  211.     value = dct_coeff_tbl[index];                                       \
  212.     run = value >> RUN_SHIFT;                                           \
  213.     if (run != END_OF_BLOCK) {                                          \
  214.       /* num_bits = (value & NUM_MASK) + 1; */                          \
  215.       /* flush_bits(num_bits); */                                       \
  216.       if (run != ESCAPE) {                                              \
  217.          /* get_bits1(value); */                                        \
  218.          /* if (value) level = -level; */                               \
  219.          flushed = (value & NUM_MASK) + 2;                              \
  220.          level = (value & LEVEL_MASK) >> LEVEL_SHIFT;                   \
  221.          value = next32bits >> (32-flushed);                            \
  222.          value &= 0x1;                                                  \
  223.          if (value) level = -level;                                     \
  224.          /* next32bits &= ((~0) >> flushed);  last op before update */  \
  225.        }                                                                \
  226.        else {    /* run == ESCAPE */                                    \
  227.          /* Get the next six into run, and next 8 into temp */          \
  228.          /* get_bits14(temp); */                                        \
  229.          flushed = (value & NUM_MASK) + 1;                              \
  230.          temp = next32bits >> (18-flushed);                             \
  231.          /* Normally, we'd ad 14 to flushed, but I've saved a few       \
  232.           * instr by moving the add below */                            \
  233.          temp &= 0x3fff;                                                \
  234.          run = temp >> 8;                                               \
  235.          temp &= 0xff;                                                  \
  236.          if (temp == 0) {                                               \
  237.             /* get_bits8(level); */                                     \
  238.             level = next32bits >> (10-flushed);                         \
  239.             level &= 0xff;                                              \
  240.             flushed += 22;                                              \
  241.             decoders_assert(level >= 128);                                      \
  242.          } else if (temp != 128) {                                      \
  243.             /* Grab sign bit */                                         \
  244.             flushed += 14;                                              \
  245.             level = ((int) (temp << 24)) >> 24;                         \
  246.          } else {                                                       \
  247.             /* get_bits8(level); */                                     \
  248.             level = next32bits >> (10-flushed);                         \
  249.             level &= 0xff;                                              \
  250.             flushed += 22;                                              \
  251.             level = level - 256;                                        \
  252.             decoders_assert(level <= -128 && level >= -255);                    \
  253.          }                                                              \
  254.        }                                                                \
  255.        /* Update bitstream... */                                        \
  256.        flush_bits(flushed);                                             \
  257.        decoders_assert (flushed <= 32);                                         \
  258.     }                                                                   \
  259.   }                                                                     \
  260.   else {                                                                \
  261.     if (index == 2) {                                                   \
  262.       /* show_bits10(index); */                                         \
  263.       index = next32bits >> 22;                                         \
  264.       value = dct_coeff_tbl_2[index & 3];                               \
  265.     }                                                                   \
  266.     else if (index == 3) {                                              \
  267.       /* show_bits10(index); */                                         \
  268.       index = next32bits >> 22;                                         \
  269.       value = dct_coeff_tbl_3[index & 3];                               \
  270.     }                                                                   \
  271.     else if (index) {   /* index == 1 */                                \
  272.       /* show_bits12(index); */                                         \
  273.       index = next32bits >> 20;                                         \
  274.       value = dct_coeff_tbl_1[index & 15];                              \
  275.     }                                                                   \
  276.     else {   /* index == 0 */                                           \
  277.       /* show_bits16(index); */                                         \
  278.       index = next32bits >> 16;                                         \
  279.       value = dct_coeff_tbl_0[index & 255];                             \
  280.     }                                                                   \
  281.     run = value >> RUN_SHIFT;                                           \
  282.     level = (value & LEVEL_MASK) >> LEVEL_SHIFT;                        \
  283.                                                                         \
  284.     /*                                                                  \
  285.      * Fold these operations together to make it fast...                \
  286.      */                                                                 \
  287.     /* num_bits = (value & NUM_MASK) + 1; */                            \
  288.     /* flush_bits(num_bits); */                                         \
  289.     /* get_bits1(value); */                                             \
  290.     /* if (value) level = -level; */                                    \
  291.                                                                         \
  292.     flushed = (value & NUM_MASK) + 2;                                   \
  293.     value = next32bits >> (32-flushed);                                 \
  294.     value &= 0x1;                                                       \
  295.     if (value) level = -level;                                          \
  296.                                                                         \
  297.     /* Update bitstream ... */                                          \
  298.     flush_bits(flushed);                                                \
  299.     decoders_assert (flushed <= 32);                                            \
  300.   }                                                                     \
  301. }
  302.  
  303. #define DecodeDCTCoeffFirst(runval, levelval)         \
  304. {                                                     \
  305.   DecodeDCTCoeff(dct_coeff_first, runval, levelval);  \
  306. }          
  307.  
  308. #define DecodeDCTCoeffNext(runval, levelval)          \
  309. {                                                     \
  310.   DecodeDCTCoeff(dct_coeff_next, runval, levelval);   \
  311. }
  312.  
  313. /*
  314.  *--------------------------------------------------------------
  315.  *
  316.  * DecodeMBAddrInc --
  317.  *
  318.  *      Huffman Decoder for macro_block_address_increment; the location
  319.  *      in which the result will be placed is being passed as argument.
  320.  *      The decoded value is obtained by doing a table lookup on
  321.  *      mb_addr_inc.
  322.  *
  323.  * Results:
  324.  *      The decoded value for macro_block_address_increment or ERROR
  325.  *      for unbound values will be placed in the location specified.
  326.  *
  327.  * Side effects:
  328.  *      Bit stream is irreversibly parsed.
  329.  *
  330.  *--------------------------------------------------------------
  331.  */
  332. #define DecodeMBAddrInc(val)                            \
  333. {                                                       \
  334.     unsigned int index;                                 \
  335.     show_bits11(index);                                 \
  336.     val = mb_addr_inc[index].value;                     \
  337.     flush_bits(mb_addr_inc[index].num_bits);            \
  338. }
  339.  
  340. /*
  341.  *--------------------------------------------------------------
  342.  *
  343.  * DecodeMotionVectors --
  344.  *
  345.  *      Huffman Decoder for the various motion vectors, including
  346.  *      motion_horizontal_forward_code, motion_vertical_forward_code,
  347.  *      motion_horizontal_backward_code, motion_vertical_backward_code.
  348.  *      Location where the decoded result will be placed is being passed
  349.  *      as argument. The decoded values are obtained by doing a table
  350.  *      lookup on motion_vectors.
  351.  *
  352.  * Results:
  353.  *      The decoded value for the motion vector or ERROR for unbound
  354.  *      values will be placed in the location specified.
  355.  *
  356.  * Side effects:
  357.  *      Bit stream is irreversibly parsed.
  358.  *
  359.  *--------------------------------------------------------------
  360.  */
  361.  
  362. #define DecodeMotionVectors(value)                      \
  363. {                                                       \
  364.   unsigned int index;                                   \
  365.   show_bits11(index);                                   \
  366.   value = motion_vectors[index].code;                   \
  367.   flush_bits(motion_vectors[index].num_bits);           \
  368. }
  369. /*
  370.  *--------------------------------------------------------------
  371.  *
  372.  * DecodeMBTypeB --
  373.  *
  374.  *      Huffman Decoder for macro_block_type in bidirectionally-coded
  375.  *      pictures;locations in which the decoded results: macroblock_quant,
  376.  *      macroblock_motion_forward, macro_block_motion_backward,
  377.  *      macroblock_pattern, macro_block_intra, will be placed are
  378.  *      being passed as argument. The decoded values are obtained by
  379.  *      doing a table lookup on mb_type_B.
  380.  *
  381.  * Results:
  382.  *      The various decoded values for macro_block_type in
  383.  *      bidirectionally-coded pictures or ERROR for unbound values will
  384.  *      be placed in the locations specified.
  385.  *
  386.  * Side effects:
  387.  *      Bit stream is irreversibly parsed.
  388.  *
  389.  *--------------------------------------------------------------
  390.  */
  391. #define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra)        \
  392. {                                                                       \
  393.   show_bits6(mb_scratch);                                               \
  394.                                                                         \
  395.   quant = mb_type_B[mb_scratch].mb_quant;                               \
  396.   motion_fwd = mb_type_B[mb_scratch].mb_motion_forward;                 \
  397.   motion_bwd = mb_type_B[mb_scratch].mb_motion_backward;                \
  398.   pat = mb_type_B[mb_scratch].mb_pattern;                               \
  399.   intra = mb_type_B[mb_scratch].mb_intra;                               \
  400.   flush_bits(mb_type_B[mb_scratch].num_bits);                           \
  401. }
  402. /*
  403.  *--------------------------------------------------------------
  404.  *
  405.  * DecodeMBTypeI --
  406.  *
  407.  *      Huffman Decoder for macro_block_type in intra-coded pictures;
  408.  *      locations in which the decoded results: macroblock_quant,
  409.  *      macroblock_motion_forward, macro_block_motion_backward,
  410.  *      macroblock_pattern, macro_block_intra, will be placed are
  411.  *      being passed as argument.
  412.  *
  413.  * Results:
  414.  *      The various decoded values for macro_block_type in intra-coded
  415.  *      pictures or ERROR for unbound values will be placed in the
  416.  *      locations specified.
  417.  *
  418.  * Side effects:
  419.  *      Bit stream is irreversibly parsed.
  420.  *
  421.  *--------------------------------------------------------------
  422.  */
  423. #define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra)        \
  424. {                                                                       \
  425.   static int quantTbl[4] = {ERROR, 1, 0, 0};                            \
  426.                                                                         \
  427.   show_bits2(mb_scratch);                                               \
  428.                                                                         \
  429.   motion_fwd = 0;                                                       \
  430.   motion_bwd = 0;                                                       \
  431.   pat = 0;                                                              \
  432.   intra = 1;                                                            \
  433.   quant = quantTbl[mb_scratch];                                         \
  434.   if (mb_scratch) {                                                     \
  435.     flush_bits (1 + quant);                                             \
  436.   }                                                                     \
  437. }
  438. /*
  439.  *--------------------------------------------------------------
  440.  *
  441.  * DecodeMBTypeP --
  442.  *
  443.  *      Huffman Decoder for macro_block_type in predictive-coded pictures;
  444.  *      locations in which the decoded results: macroblock_quant,
  445.  *      macroblock_motion_forward, macro_block_motion_backward,
  446.  *      macroblock_pattern, macro_block_intra, will be placed are
  447.  *      being passed as argument. The decoded values are obtained by
  448.  *      doing a table lookup on mb_type_P.
  449.  *
  450.  * Results:
  451.  *      The various decoded values for macro_block_type in
  452.  *      predictive-coded pictures or ERROR for unbound values will be
  453.  *      placed in the locations specified.
  454.  *
  455.  * Side effects:
  456.  *      Bit stream is irreversibly parsed.
  457.  *
  458.  *--------------------------------------------------------------
  459.  */
  460. #define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra)        \
  461. {                                                                       \
  462.                                                                         \
  463.   show_bits6(mb_scratch);                                               \
  464.                                                                         \
  465.   quant = mb_type_P[mb_scratch].mb_quant;                               \
  466.   motion_fwd = mb_type_P[mb_scratch].mb_motion_forward;                 \
  467.   motion_bwd = mb_type_P[mb_scratch].mb_motion_backward;                \
  468.   pat = mb_type_P[mb_scratch].mb_pattern;                               \
  469.   intra = mb_type_P[mb_scratch].mb_intra;                               \
  470.                                                                         \
  471.   flush_bits(mb_type_P[mb_scratch].num_bits);                           \
  472. }
  473. /*
  474.  *--------------------------------------------------------------
  475.  *
  476.  * DecodeCBP --
  477.  *
  478.  *      Huffman Decoder for coded_block_pattern; location in which the
  479.  *      decoded result will be placed is being passed as argument. The
  480.  *      decoded values are obtained by doing a table lookup on
  481.  *      coded_block_pattern.
  482.  *
  483.  * Results:
  484.  *      The decoded value for coded_block_pattern or ERROR for unbound
  485.  *      values will be placed in the location specified.
  486.  *
  487.  * Side effects:
  488.  *      Bit stream is irreversibly parsed.
  489.  *
  490.  *--------------------------------------------------------------
  491.  */
  492. #define DecodeCBP(coded_bp)                                             \
  493. {                                                                       \
  494.   show_bits9(mb_scratch);                                               \
  495.   coded_bp = coded_block_pattern[mb_scratch].cbp;                       \
  496.   flush_bits(coded_block_pattern[mb_scratch].num_bits);                 \
  497. }
  498.